<?php
namespace App\Http\Requests\Trade\Supply;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Models\Trade\Supply\SupplyModel;
use App\Rules\Mobile as MobileRule;

class ItemRequest extends FormRequest
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => 'nullable|integer|min:1',
            'kind_id' => 'required|integer|min:1',
            'title' => 'nullable|string|max:16',
            'size_min' => 'nullable|numeric|between:0.01,10000',
            'size_max' => 'nullable|numeric|between:0.01,10000',
            'size_unit_id' => 'required|integer',
            'sale_type' => [
                'required',
                Rule::in(SupplyModel::getSaleTypeList())
            ],
            'sale_pre.year' => 'required_if:sale_type,2|integer|between:2015,2030',
            'sale_pre.month' => 'required_if:sale_type,2|integer|between:1,12',
            'sale_pre.day' => [
                'required_if:sale_type,2',
                Rule::in(SupplyModel::getSalePresaleDayList())
            ],
            'price_type' => [
                'required',
                Rule::in(SupplyModel::getPriceTypeList())
            ],
            'price' => 'required_if:price_type,1|numeric|between:0.01,10000',
            'price_unit_id' => 'required|integer|min:1',
            'stock' => 'required|numeric|between:0.01,100000000',
            'province' => 'required|integer|min:1',
            'city' => 'required|integer|min:1',
            'area' => 'required|integer|min:1',
            'town' => 'required_unless:province,1,2,9,22|integer|min:1',
            'address' => 'nullable|string|max:50',
            'valided' => 'required|integer|between:1420041600,1893427200', // 2015~2030
            'desc' => 'nullable|string|max:50',
            'desc_tag' => 'nullable|array|between:1,3',
            'img' => 'required|array|between:1,9',
            'contact_name' => 'required|string|between:1,20',
            'contact_mobile' => [
                'required',
                new MobileRule()
            ]
        ];
    }

    /**
     * 获取已定义的验证规则的错误消息。
     *
     * @return array
     */
    public function messages()
    {
        return [
            'sale_type.in' => ':attribute 必须在[' . implode(',', SupplyModel::getSaleTypeList()) . ']中',
            'sale_pre.day.in' => ':attribute 必须在[' . implode(',', SupplyModel::getSalePresaleDayList()) . ']中',
            'price_type.in' => ':attribute 必须在[' . implode(',', SupplyModel::getPriceTypeList()) . ']中',
            'town.required_unless' => '当 province 不为 [1,2,9,22] 时 town 不能为空'
        ];
    }

    /**
     * 获取已定义的验证规则的字段描述。
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'id' => '数据 ID',
            'kind_id' => '品种',
            'title' => '标题',
            'size_min' => '最小规格',
            'size_max' => '最大规格',
            'size_unit_id' => '规格单位',
            'sale_type' => '出售类型',
            'sale_pre.year' => '预售年份',
            'sale_pre.month' => '预售月份',
            'sale_pre.day' => '预售时间',
            'price_type' => '价格类型',
            'price' => '价格',
            'price_unit_id' => '价格单位',
            'stock' => '出售量',
            'province' => '省份',
            'city' => '城市',
            'area' => '区县',
            'town' => '乡镇',
            'address' => '详细地址',
            'valided' => '有效期至',
            'desc' => '描述',
            'desc_tag' => '描述标签',
            'img' => '图片',
            'contact_name' => '联系人',
            'contact_mobile' => '联系电话'
        ];
    }
}
